home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1999 November / Macworld (1999-11).dmg / Updaters / WhiteCap 3.0.4 / WhiteCap Source.sit / WhiteCap Source / Common / io / CEgFileSpec.cpp < prev    next >
C/C++ Source or Header  |  1999-07-31  |  6KB  |  359 lines

  1. #include "CEgFileSpec.h"
  2.  
  3. #include "CEgIOFile.h"
  4.  
  5. #ifdef EG_WIN
  6. #include <stdio.h>
  7.  
  8. #ifndef GetCurrentDirectory
  9. #include <windows.h>
  10. #endif
  11.  
  12. #endif
  13.  
  14. #include "EgOSUtils.h"
  15.  
  16.  
  17. #ifdef EG_MAC
  18. #include <Resources.h>
  19. #include <Files.h>
  20. #endif
  21.  
  22.  
  23. CEgFileSpec::CEgFileSpec() {
  24.     
  25.     Assign( NULL, 0 );
  26. }
  27.  
  28.  
  29. CEgFileSpec::CEgFileSpec( const char* inFileName, long inType  ) {
  30.     Assign( NULL, 0 );
  31.     AssignPathName( inFileName );
  32.     SetType( inType );
  33. }
  34.  
  35.  
  36.  
  37.  
  38. CEgFileSpec::CEgFileSpec( const CEgFileSpec& inSpec ) {
  39.     
  40.     Assign( inSpec );
  41. }
  42.  
  43.  
  44.  
  45.  
  46. void CEgFileSpec::Assign( const CEgFileSpec& inSpec ) {
  47.  
  48.     Assign( inSpec.OSSpec(), inSpec.GetType() );
  49. }
  50.  
  51.  
  52.  
  53.  
  54.  
  55. void CEgFileSpec::Assign( const void* inOSSpecPtr, long inType ) {
  56.     mFileType = inType;
  57.     
  58.     #ifdef EG_MAC
  59.     mSpecData.Assign( (char*) inOSSpecPtr, sizeof( FSSpec ) + 32 );
  60.     #endif
  61.     #ifdef EG_WIN    
  62.     mSpecData.Assign( (char*) inOSSpecPtr );
  63.     #endif
  64. }
  65.  
  66.  
  67. void CEgFileSpec::AssignPathName( const char* inPathName ) {
  68.  
  69.     if ( inPathName ) {
  70.         #ifdef EG_MAC
  71.         FSSpec    spec;
  72.         UtilStr path( inPathName );
  73.         ::FSMakeFSSpec( 0, 0, path.getPasStr(), &spec );
  74.         //__path2fss( inPathName, &spec );
  75.         Assign( &spec, 0 );
  76.         #endif
  77.         #ifdef EG_WIN    
  78.         Assign( inPathName, 0 );
  79.         #endif
  80.     }    
  81. }
  82.  
  83.  
  84. void CEgFileSpec::AssignFolder( const char* inFolderName ) {
  85.     
  86.     #ifdef EG_MAC
  87.     CInfoPBRec     pb;
  88.     FSSpec        spec;
  89.     Str255        name;
  90.     
  91.     UtilStr path;
  92.     if ( inFolderName[ 0 ] != ':' )
  93.         path.Append( ':' );
  94.     path.Append( inFolderName );
  95.     path.copyTo( name, 255 );
  96.     pb.hFileInfo.ioNamePtr        = name;
  97.     pb.hFileInfo.ioVRefNum        = ( (FSSpec*) EgOSUtils::sAppSpec.OSSpec() ) -> vRefNum;
  98.     pb.hFileInfo.ioDirID        = ( (FSSpec*) EgOSUtils::sAppSpec.OSSpec() ) -> parID;
  99.     pb.hFileInfo.ioFDirIndex    = 0;
  100.  
  101.     ::PBGetCatInfoSync( &pb );
  102.     spec.vRefNum    = pb.hFileInfo.ioVRefNum;
  103.     spec.parID        = pb.hFileInfo.ioDirID;
  104.     spec.name[0]    = 0;
  105.     Assign( &spec, 0 );
  106.     #endif
  107.     
  108.     #ifdef EG_WIN
  109.     mSpecData.Assign( (char*) EgOSUtils::sAppSpec.OSSpec() );
  110. //    if ( inFolderName[ 0 ] != '\\' )
  111. //        mSpecData.Append( '\\' );
  112.     mSpecData.Append( inFolderName );
  113.     if ( mSpecData.getChar( mSpecData.length() ) != '\\' )
  114.         mSpecData.Append( '\\' );
  115.     #endif
  116. }
  117.     
  118.  
  119.  
  120.  
  121.  
  122. long CEgFileSpec::GetType() const {
  123.  
  124.     #ifdef EG_MAC
  125.     return mFileType;
  126.     #endif
  127.     
  128.     #ifdef EG_WIN
  129.     unsigned long i, j, len, type = 0;
  130.  
  131.     len = mSpecData.length();
  132.     i = mSpecData.FindLastInstanceOf( '.' );
  133.     j = mSpecData.FindLastInstanceOf( '\\' );
  134.     
  135.     if ( i > 0 && len - i <= 3 && j < i ) {
  136.         for ( j = i; j <= len; j++ )
  137.             type = ( type << 8 ) | mSpecData.getChar( j );
  138.     }
  139.     
  140.     return type;
  141.     #endif    
  142.     
  143. }
  144.  
  145.  
  146.  
  147. void CEgFileSpec::TypeToExt( UtilStr& ioStr, long inType ) {
  148.     long c, i, pos = ioStr.length();
  149.     
  150.     for ( i = 0; i < 4; i++ ) {
  151.         c = (inType >> i * 8) & 0xFF;
  152.         if ( c > 32 )
  153.             ioStr.Insert( pos, c, 1 );
  154.     }
  155.     if ( ioStr.length() > pos ) {
  156.         if ( ioStr.getChar( pos + 1 ) != '.' )
  157.             ioStr.Insert( pos, '.', 1 );
  158.     }
  159. }
  160.  
  161.  
  162.  
  163. void CEgFileSpec::SetType( long inType ) {
  164.     mFileType = inType;
  165. }
  166.  
  167.  
  168.  
  169.  
  170.  
  171. void CEgFileSpec::Delete() const {
  172.     
  173.     if ( OSSpec() ) {
  174.         #ifdef EG_MAC
  175.         ::FSpDelete( (FSSpec*) mSpecData.getCStr() );
  176.         #endif
  177.         #ifdef EG_WIN
  178.         remove( mSpecData.getCStr() );
  179.         #endif
  180.     }
  181. }
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188. void CEgFileSpec::Rename( const UtilStr& inNewName ) {
  189.  
  190.     
  191.     #ifdef EG_MAC
  192.     inNewName.copyTo( ((FSSpec*) OSSpec()) -> name, 32 );
  193.     #endif
  194.     
  195.     #ifdef EG_WIN
  196.     int pos;
  197.     pos = mSpecData.FindLastInstanceOf( '\\' );
  198.     mSpecData.Trunc( mSpecData.length() - pos );
  199.     mSpecData.Append( inNewName );
  200.     #endif
  201. }
  202.  
  203.  
  204.  
  205. void CEgFileSpec::GetFileName( UtilStr& outFileName ) const {
  206.     int     pos;
  207.     
  208.     outFileName.Wipe();
  209.         
  210.     if ( OSSpec() ) {
  211.         #ifdef EG_MAC
  212.         outFileName.Assign( ((FSSpec*) OSSpec()) -> name );
  213.         #endif
  214.         #ifdef EG_WIN
  215.         
  216.         // If it's a folder, temporarily remove the end '\'
  217.         long len = mSpecData.length();
  218.         bool isFolder = mSpecData.getChar( len ) == '\\';
  219.         if ( isFolder ) {
  220.             pos = mSpecData.FindPrevInstanceOf( len - 1, '\\' ); 
  221.             outFileName.Assign( mSpecData.getCStr() + pos, len - pos - 1 );
  222.             }
  223.         else {
  224.             pos = mSpecData.FindLastInstanceOf( '\\' );
  225.             outFileName.Assign( mSpecData.getCStr() + pos );
  226.         }
  227.         #endif
  228.     }
  229.     
  230.     pos = outFileName.FindLastInstanceOf( '.' );    // Find where ext begins
  231.     
  232.     if ( pos > 0 )
  233.         outFileName.Keep( pos - 1 );                // Strip extension
  234. }
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241. void CEgFileSpec::SaveAs( const CEgFileSpec& inDestSpec ) const {
  242.     
  243.     if ( OSSpec() && inDestSpec.OSSpec() ) {
  244.         #ifdef EG_MAC
  245.         ::FSpExchangeFiles ( (FSSpec*) OSSpec(), (FSSpec*) inDestSpec.OSSpec() );
  246.         Delete();
  247.         #endif
  248.         #ifdef EG_WIN
  249.         inDestSpec.Delete();
  250.         rename( (char*) OSSpec(), (char*) inDestSpec.OSSpec() );
  251.         #endif
  252.     }
  253. }
  254.  
  255.  
  256.  
  257. int CEgFileSpec::Exists() const {
  258.     CEgIFile      iFile;
  259.  
  260.     #if EG_MAC
  261.     OSErr                err;
  262.     HFileInfo            pb;
  263.     FSSpec*             spec = (FSSpec*) OSSpec();
  264.     UtilStr                fname;
  265.     
  266.     // Prep a str to receive un updated name change
  267.     fname.Dim( 40 );
  268.     fname.Assign( spec -> name );
  269.     
  270.     // See if we have a file
  271.     pb.ioDirID         = spec -> parID;
  272.     pb.ioVRefNum    = spec -> vRefNum;
  273.     pb.ioNamePtr     = fname.getPasStr();
  274.     pb.ioFDirIndex    = 0;
  275.     err = ::PBGetCatInfoSync( (CInfoPBRec*) &pb );
  276.     if ( err == noErr )
  277.         return 1;
  278.     
  279.     // See if we have a dir    
  280.     pb.ioDirID         = spec -> parID;
  281.     pb.ioVRefNum    = spec -> vRefNum;
  282.     pb.ioFDirIndex    = -1;
  283.     err = ::PBGetCatInfoSync( (CInfoPBRec*) &pb );
  284.     if ( err == noErr && fname.compareTo( spec -> name, false ) == 0 )
  285.         return 2;
  286.     #endif
  287.     
  288.     #if EG_WIN
  289.     long attribs = ::GetFileAttributes( (char*) OSSpec() );
  290.  
  291.     if ( attribs & FILE_ATTRIBUTE_DIRECTORY )
  292.         return 2;
  293.     else if ( attribs & FILE_ATTRIBUTE_NORMAL )
  294.         return 1;
  295.     #endif
  296.  
  297.     return 0;
  298. }
  299.  
  300.  
  301.  
  302. CEgErr CEgFileSpec::Duplicate( const CEgFileSpec& inDestSpec ) const {
  303.     CEgIOFile    oFile;
  304.     CEgIFile    iFile;
  305.  
  306.     // Open the source and destination files
  307.     iFile.open( this );
  308.     iFile.seek( 0 );
  309.     if ( ! iFile.noErr() ) 
  310.         oFile.open( &inDestSpec );
  311.     
  312.     long        pos        = 0, numBytes;
  313.     long        size    = iFile.size();
  314.     CEgErr        err;
  315.     
  316.     // Copy the file in 50k chunks
  317.     while ( pos < size && oFile.noErr() && iFile.noErr() ) {
  318.         numBytes = 50000;
  319.         if ( numBytes + pos > size )
  320.             numBytes = size - pos;
  321.         oFile.CEgOStream::PutBlock( iFile, numBytes );
  322.         pos += numBytes;
  323.     }
  324.     
  325.     if ( iFile.noErr() )
  326.         err = oFile;
  327.     else
  328.         err = iFile;
  329.         
  330.     return err;
  331. }
  332.  
  333.  
  334.  
  335.  
  336. void CEgFileSpec::ChangeExt( const char* inExt ) {
  337.     UtilStr    fileName;
  338.     long        len;
  339.     
  340.     GetFileName( fileName );
  341.     fileName.Append( '.' );
  342.     len = fileName.length();
  343.     fileName.Append( inExt );
  344.     fileName.Keep( len + 3 );
  345.     Rename( fileName );
  346.     
  347. }
  348.  
  349.  
  350.  
  351.  
  352.  
  353. const void* CEgFileSpec::OSSpec() const {
  354.  
  355.     if ( mSpecData.length() > 0 )
  356.         return mSpecData.getCStr();
  357.     else
  358.         return NULL;
  359. }